﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.Text;
using System.Security;
using System.Web.Security;

using Webchatting.Dal;
using System.Configuration;
using System.Data.OleDb;
using System.Web;
using Webchatting.Common;
using System.Collections;


namespace Webchatting.Dal
{
    /// <summary>
    /// 消息接收类
    /// </summary>
    public class ReceiveMessages
    {
        #region 变量
        public static readonly string connStr = (ConfigurationManager.ConnectionStrings["connStr"].ConnectionString);
        public SqlConnection ConnSql
        {
            set;
            get;
        }
        #endregion

        /// <summary>
        /// 构造函数，用于初始化 
        /// </summary>
        public ReceiveMessages()
        {
            //初始化数据库连接
            this.ConnSql = new SqlConnection(connStr);
        }


        //接收好友消息
        public string ReceiveFreindsMessagesMethod(string RUMToRUId, string RUId, string isUpdateChatMessages)
        {
            string receiveMessages = string.Empty;
            SqlParameter[] sqlParams = new SqlParameter[2];
            sqlParams[0] = new SqlParameter("@RUMToRUId", RUMToRUId);
            sqlParams[1] = new SqlParameter("@RUId", RUId);
            SqlDataReader dr = SqlHelper.ExecuteReader(ConnSql, "spReceiveFriendsMessages", sqlParams);
            while (dr.Read())
            {
                receiveMessages += "<li>" + dr["RUNickName"].ToString().Trim() + " 在 " + dr["RUMSendTime"].ToString().Trim() + " 说<br/>" + dr["RUMContent"].ToString().Trim() + "</li>";
            }
            dr.Close();
            ConnSql.Close();
            if (isUpdateChatMessages.Trim() == "true")
            {
                SqlHelper.ExecuteNonQuery(ConnSql, "spUpdateChatMessages", sqlParams);
                ConnSql.Close();
            }
            return receiveMessages;
        }

        public string ReceiveFriendsMessagesReadOnly(string RUMToRUId, string RUId, string isUpdateChatMessages)
        {
            string receiveMessages = string.Empty;
            SqlParameter[] sqlParams = new SqlParameter[2];
            sqlParams[0] = new SqlParameter("@RUMToRUId", RUMToRUId);
            sqlParams[1] = new SqlParameter("@RUId", RUId);
            SqlDataReader dr = SqlHelper.ExecuteReader(ConnSql, "spReceiveMessages", sqlParams);

            while (dr.Read())
            {
                receiveMessages += dr["RUId"].ToString().Trim();
            }
            dr.Close();
            ConnSql.Close();
            return receiveMessages;
        }
      


        //接收群消息
        public string ReceiveGroupsMessagesMethod(string GroupId,string RUId)
        {
            string receiveGroupMessages = string.Empty;

           
            SqlParameter[] sqlParams = new SqlParameter[2];
            sqlParams[0] = new SqlParameter("@GroupId", GroupId);
            sqlParams[1] = new SqlParameter("@RUId", RUId);
            
            SqlDataReader dr = SqlHelper.ExecuteReader(ConnSql, "spReceiveGroupMsgs", sqlParams);
            while (dr.Read())
            {
                receiveGroupMessages += "<li>" + dr["RUGroupRemarkName"].ToString().Trim() + "(" + dr["RUId"].ToString().Trim() + ")" + "   在   " + dr["GMSendTime"].ToString().Trim() + "  说<br/>" + dr["GMContent"].ToString().Trim() + "</li>";
            }
            
            dr.Close();
            SqlParameter[] sqlParams2 = new SqlParameter[1];
            sqlParams2[0] = new SqlParameter("@RUId", RUId);
            SqlHelper.ExecuteNonQuery(ConnSql, CommandType.StoredProcedure,
            "spUpdateLeaveTime", sqlParams2);
            
            
            ConnSql.Close();

            return receiveGroupMessages;
        }

    }
}